﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace zytech.common.baseform.sys
{
    public partial class PwdModify : Form
    {
        protected string SqlConnStr = "";
        public PwdModify()
        {
            InitializeComponent();
        }

        private void clearall()
        {
            label4.Visible = false;
            label5.Visible = false;
            label6.Visible = false;
            label8.Visible = false;
            label7.Text = "";
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox1.Focus();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            label7.Text = "";
            label4.Visible = false;
            label5.Visible = false;
            label6.Visible = false;
            label8.Visible = false;
            string pwd, opwd, npwd1, npwd2;
            opwd = textBox1.Text;
            npwd1 = textBox2.Text;
            npwd2 = textBox3.Text;
            if (opwd == null | opwd == "")
            { label7.Text += "原始密码不能为空！"; label4.Visible = true; textBox1.Focus(); return; }
            if (npwd1 == null | npwd1 == "")
            { label7.Text += "请输入新密码！"; label5.Visible = true; textBox2.Focus(); return; }
            if (npwd2 == null | npwd2 == "")
            {
                label7.Text += "请确认新密码！";
                label6.Visible = true;
                if (npwd1 == null | npwd1 == "")
                    textBox2.Focus();
                else
                    textBox3.Focus();
                return;
            }
            if (npwd1 != null && npwd1 != "" && npwd2 != null && npwd2 != "" && npwd1 != npwd2)
            {
                label7.Text += "新密码输入不一致！";
                label8.Visible = true;
                textBox3.Text = "";
                textBox2.Text = "";
                textBox2.Focus();
                return;
            }
            string id = LoginForm.name;
            string sql = "select cPassword from sys_user where cUserid = '" + id + "'";
            string SqlConnStr = ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString;
            SqlConnection conn = new SqlConnection(SqlConnStr);
            SqlCommand com1 = new SqlCommand(sql, conn);
            SqlDataReader dr = null;
            conn.Open();
            dr = com1.ExecuteReader();
            if (dr.Read())
            {
                pwd = dr["cPassword"].ToString();
                if (opwd != null && opwd != "" && pwd != opwd)
                {
                    label7.Text += "原始密码输入错误！";
                    label8.Visible = true;
                    textBox1.Text = "";
                    textBox1.Focus();
                    return;
                }
            }
            dr.Close();
            string str = "update sys_user set cPassword = '" + npwd1 + "' where cUserid = '" + id + "'";
            SqlCommand com2 = new SqlCommand(str, conn);
            com2.ExecuteNonQuery();
            MessageBox.Show("密码修改成功！");
            conn.Close();
            clearall();
        }

        private void PwdModify_Load(object sender, EventArgs e)
        {
            clearall();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.Send("{TAB}");
            }
        }

        private void textBox2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.Send("{TAB}");
            }
        }

        private void textBox3_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.button1_Click(sender, e);
            }
        }
    }
}
